#define WIN32_LEAN_AND_MEAN
#include <WinSock2.h>
#include <windows.h>
#include<string.h>
#include <iostream>
#include<vector>
#pragma warning(disable:4996)
#pragma comment(lib,"ws2_32.lib")

using namespace std;
int main()
{
	char username[10] = {};
	char password[20] = {};
	char mode[2] = {};
	cout << "please input mode(1.manager 2.user)\n";
	cin >> mode;
	cout << "pealse enter name\n";
	cin >> username;
	cout << "please enter password\n";
	cin >> password;


	int iRet;


	WORD ver = MAKEWORD(2, 2);//使用版本2.2
	WSADATA data;
	WSAStartup(ver, &data);//参数

	//1、创建套接字
	SOCKET sock = socket(AF_INET, SOCK_STREAM, 0); //不用声明tcp
	//2、连接服务器，绑定网络端口
	sockaddr_in sin = {};
	sin.sin_family = AF_INET;								//ipv4
	sin.sin_port = htons(6666);								//host to net  unsigned short   服务器端口号
	sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");		//服务器地址;
	iRet = connect(sock, (sockaddr*)&sin, sizeof(sockaddr_in));

	if (iRet == SOCKET_ERROR)
	{
		cout << "connect fail" << endl;
	}
	else
	{
		cout << "connect success" << endl;
	}

	//3、recv 接收服务器信息
	/*
	int
	WSAAPI
	recv(
		_In_ SOCKET s,
		_Out_writes_bytes_to_(len, return) __out_data_source(NETWORK) char FAR * buf,
		_In_ int len,
		_In_ int flags
		);
	*/
	
		char ch[30] = {};
		strcpy(ch, mode);
		strcat(ch, " ");
		strcat(ch, username);
		strcat(ch, " ");
		strcat(ch, password);

		iRet = send(sock, ch, strlen(ch) + 1, 0);
		
		
		char resbuf[256] = {};
		iRet = recv(sock, resbuf, 256, 0);
		


		if (strlen(resbuf) == 10)
		{
			cout << "ID:" << resbuf;
		}

		else if (strlen(resbuf) == 14)
		{
			cout << resbuf << endl;
		}

		else if (strlen(resbuf) == 16)
		{
			char id[11];
			cout << "please input key" << endl;
			cin >> id;
			if (strlen(id) == 10)
			{
				iRet = send(sock, id, strlen(id) + 1, 0);

				char resbuf[256] = {};
				iRet = recv(sock, resbuf, 256, 0);
				cout << resbuf << endl;
			}
			else
			{
				cout << "wrong id" << endl;
			}
		}
	//4、清除socket环境
	iRet = closesocket(sock);

	if (iRet == SOCKET_ERROR)
	{
		cout << "closesocket fail" << endl;
	}
	else
	{
		cout << "closesocket success" << endl;
	}

	WSACleanup();//清除
	
}